<?php

namespace App\Models\Issue;

class OptionService
{
    protected $repository;

    public function __construct(OptionRepository $repository)
    {
        $this->repository = $repository;
    }

    public function createAndGetId(array $data)
    {
        $option = new IssueOption();
        $option->fill($data);

        $saved = $this->repository->store($option);

        return $saved->id;
    }

    public function update($id, $data)
    {
        $option = $this->repository->getOneById($id);

        if (is_null($option)) {
            abort(404);
        }

        return $option->fill($data)->save();
    }

    public function delete($id)
    {
        $option = $this->repository->getOneById($id);

        if (is_null($option)) {
            abort(404);
        }

        return $option->delete();
    }

    public function getByIssue($issueId)
    {
        return $this->repository->getByIssue($issueId);
    }

    public function getById($id)
    {
        return $this->repository->getOneById($id);
    }

    public function deleteByIssue($issueId)
    {
        return $this->repository->deleteByIssue($issueId);
    }
}
